﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Model.Products;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure.Messaging;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Website.Infrastructure;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class UnitsController : ControllerBase
    {
        #region Index
        //
        // GET: /Units/

        public ActionResult Index()
        {
            IEnumerable<Unit> units = null;
            try
            {
                ProductService.ProductServiceClient productService = new ProductService.ProductServiceClient();
                productService.AddValidationToken(this.Token);
                units = productService.GetUnitList();
                productService.Close();
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorUnitList, MessageType.Error);
            }
            return View(units);
        } 
        #endregion

        #region Create
        //
        // GET: /Units/Create

        public ActionResult Create()
        {
            return View();
        }  

        //
        // POST: /Units/Create

        [HttpPost]
        public ActionResult Create(Unit unit)
        {
            try
            {
                ProductService.ProductServiceClient productService = new ProductService.ProductServiceClient();
                productService.AddValidationToken(this.Token);
                productService.StoreUnit(unit);
                productService.Close();
                this.SetMessage(InfoStrings.UnitCreated, MessageType.Info);
                return RedirectToAction("Index");
            } 
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch(Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorUnitCreating, MessageType.Error);
            } 
            return View(unit);
        }

        #endregion

        #region Edit
        //
        // GET: /Units/Edit/5

        public ActionResult Edit(int id)
        {
            Unit unit = null;
            try
            {
                ProductService.ProductServiceClient productService = new ProductService.ProductServiceClient();
                productService.AddValidationToken(this.Token);
                unit = productService.GetUnit(id);
                productService.Close();
                return View(unit);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(InfoStrings.UnitNotFound, MessageType.Error);
            }
            return RedirectToAction("Index");
        }

        //
        // POST: /Units/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Unit unit)
        {
            try
            {
                unit.Id = id;
                ProductService.ProductServiceClient productService = new ProductService.ProductServiceClient();
                productService.AddValidationToken(this.Token);
                productService.StoreUnit(unit);
                this.SetMessage(InfoStrings.UnitEdited, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorUnitEditing, MessageType.Error);
            } 
            return View(unit);
        } 
        #endregion

        #region Delete
        //
        // GET: /Units/Delete/5

        public ActionResult Delete(int id)
        {
            return this.Edit(id);
        }

        //
        // POST: /Units/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                ProductService.ProductServiceClient productService = new ProductService.ProductServiceClient();
                productService.AddValidationToken(this.Token);
                productService.RemoveUnit(id);
                productService.Close();
                this.SetMessage(InfoStrings.UnitDeleted, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorUnitDeleting, MessageType.Error);
            }
            return RedirectToAction("Index");
        } 
        #endregion
    }
}
